using UnityEngine;

namespace MaterialUI
{
	[ExecuteInEditMode]
	[AddComponentMenu("MaterialUI/Layout/Adaptive UI Grid")]
	public class AdaptiveUIGrid : MonoBehaviour
	{
		[SerializeField]
		[Observe(new string[]
		{
			"GenerateGutterTexture"
		})]
		private Color m_GutterColor = new Color(0.588f, 0.706f, 1f, 0.5f);

		[SerializeField]
		[Popup(new object[]
		{
			8f,
			16f,
			24f,
			40f
		})]
		private float m_GutterWidth = 24f;

		[SerializeField]
		[Popup(new object[]
		{
			8f,
			16f,
			24f,
			40f
		})]
		private float m_MarginWidth = 24f;

		[SerializeField]
		[Popup(new object[]
		{
			1,
			2,
			3,
			4,
			6,
			12
		})]
		private int m_NumberOfColums = 12;

		[SerializeField]
		private bool m_FullScreenHeight;

		private Texture2D m_GutterTexture;

		private RectTransform m_RectTransform;

		private Canvas m_Canvas;

		private Rect m_Rect;

		private void OnEnable()
		{
			GenerateGutterTexture();
			m_RectTransform = GetComponent<RectTransform>();
			m_Canvas = GetComponentInParent<Canvas>();
		}

		private void GenerateGutterTexture()
		{
			if (m_GutterTexture == null)
			{
				m_GutterTexture = new Texture2D(1, 1, TextureFormat.ARGB32, mipChain: false);
				m_GutterTexture.hideFlags = HideFlags.HideAndDontSave;
			}
			m_GutterTexture.SetPixel(0, 0, m_GutterColor);
			m_GutterTexture.Apply();
		}

		private void OnDrawGizmos()
		{
			if (base.enabled)
			{
				DrawGrid(useGizmos: true);
			}
		}

		private void OnGUI()
		{
			if (!Event.current.type.Equals(EventType.Repaint))
			{
				return;
			}
			if ((bool)m_RectTransform)
			{
				Vector2 properSize = m_RectTransform.GetProperSize();
				Vector3[] array = new Vector3[4];
				m_RectTransform.GetWorldCorners(array);
				m_Rect.x = array[1].x;
				m_Rect.width = properSize.x;
				if (m_FullScreenHeight)
				{
					m_Rect.y = 0f;
					m_Rect.height = Screen.height;
				}
				else
				{
					m_Rect.y = array[1].y;
					m_Rect.y = 0f - m_Rect.y;
					m_Rect.y += Screen.height;
					m_Rect.height = properSize.y;
				}
				if ((bool)m_Canvas && m_Canvas.pixelPerfect)
				{
					m_Rect.x = Mathf.Ceil(m_Rect.x);
					m_Rect.y = Mathf.Floor(m_Rect.y);
					m_Rect.width = Mathf.Ceil(m_Rect.width);
					m_Rect.height = Mathf.Floor(m_Rect.height);
				}
				DrawGrid(useGizmos: false);
			}
			else
			{
				base.enabled = false;
			}
		}

		private void DrawGrid(bool useGizmos)
		{
			DrawTexture(new Rect(m_Rect.x, m_Rect.y, m_MarginWidth, m_Rect.height), useGizmos);
			DrawTexture(new Rect(m_Rect.x + (m_Rect.width - m_MarginWidth), m_Rect.y, m_MarginWidth, m_Rect.height), useGizmos);
			float num = m_Rect.width - m_MarginWidth * 2f;
			float num2 = num - (float)(m_NumberOfColums - 1) * m_GutterWidth;
			float num3 = num2 / (float)m_NumberOfColums;
			float num4 = m_Rect.x + m_MarginWidth;
			for (int i = 0; i < m_NumberOfColums - 1; i++)
			{
				num4 += num3;
				DrawTexture(new Rect(num4, m_Rect.y, m_GutterWidth, m_Rect.height), useGizmos);
				num4 += m_GutterWidth;
			}
		}

		private void DrawTexture(Rect rect, bool useGizmos)
		{
			if (useGizmos)
			{
				Gizmos.DrawGUITexture(rect, m_GutterTexture);
			}
			else
			{
				Graphics.DrawTexture(rect, m_GutterTexture);
			}
		}
	}
}
